package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.vo.MallCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.MallCategory;
import com.ktgj.customer.service.IMallCategoryService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 类目Controller
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/category")
public class MallCategoryController extends BaseController
{
    @Autowired
    private IMallCategoryService mallCategoryService;

    /**
     * 查询类目列表
     */
    @PreAuthorize(hasPermi = "customer:category:list")
    @GetMapping("/list")
    public TableDataInfo list(MallCategory mallCategory)
    {
        startPage();
        List<MallCategory> list = mallCategoryService.selectMallCategoryList(mallCategory);
        return getDataTable(list);
    }

    /**
     * 导出类目列表
     */
    @PreAuthorize(hasPermi = "customer:category:export")
    @Log(title = "类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallCategory mallCategory) throws IOException
    {
        List<MallCategory> list = mallCategoryService.selectMallCategoryList(mallCategory);
        ExcelUtil<MallCategory> util = new ExcelUtil<MallCategory>(MallCategory.class);
        util.exportExcel(response, list, "类目数据");
    }

    /**
     * 获取类目详细信息
     */
    @PreAuthorize(hasPermi = "customer:category:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallCategoryService.selectMallCategoryById(id));
    }

    /**
     * 新增类目
     */
    @PreAuthorize(hasPermi = "customer:category:add")
    @Log(title = "类目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallCategory mallCategory)
    {
        return toAjax(mallCategoryService.insertMallCategory(mallCategory));
    }

    /**
     * 修改类目
     */
    @PreAuthorize(hasPermi = "customer:category:edit")
    @Log(title = "类目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallCategory mallCategory)
    {
        return toAjax(mallCategoryService.updateMallCategory(mallCategory));
    }

    /**
     * 删除类目
     */
    @PreAuthorize(hasPermi = "customer:category:remove")
    @Log(title = "类目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallCategoryService.deleteMallCategoryByIds(ids));
    }

    /**
     * 树型商品类目
     * */
    @PostMapping("/mallCategoryList")
    public AjaxResult mallCategoryList() {
        List<MallCategoryVo> mallCategoryList;
        try {
            mallCategoryList = mallCategoryService.mallCategoryList();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(mallCategoryList);
    }
}
